import java.util.Arrays;

public class TestDemo {
    //Arrays的方法
    public static void main(String[] args) {
        int[] array={-1,2,39,118983,23,44444};
        System.out.println("排序前 "+Arrays.toString(array));//1.遍历打印数组 Arrays.toString
        Arrays.sort(array);//2.排序数组 Arrays.sort
        System.out.println("排序后 "+Arrays.toString(array));
        int ret1=Arrays.binarySearch(array,40);//3.（对有序数组）二分查找 Arrays.binarySearch 从array里面找40，返回下标
        System.out.println(ret1);
        int ret2=Arrays.binarySearch(array,39);
        System.out.println(ret2);
        int ret3=Arrays.binarySearch(array,1,2,23);//4.(要进行查找的数组，开始位置的下标，最后位置的下标，在两个下标之间要找的数）
        System.out.println(ret3);                                      //对于Arrays来说，区间都是前闭后开：[x,y)
        int[] copy1=Arrays.copyOf(array,array.length*2);//5. 复制或扩容 copy1=Arrays.copyOf(array,array.length*2)
        System.out.println(Arrays.toString(copy1));              //             将array复制到copy1里，array.length*2：复制的长度
        //int[] array=Arrays.copyOf(array,array.length*2);不行
        int[] array1={1,2,3,4,5};
        System.arraycopy(array1,0,copy1,0,array1.length-1);//6. 复制(A,x,B,y,z)
        System.out.println(Arrays.toString(copy1));                   //从A的x位置开始复制，从B的y位置开始接收，复制长度为z
    }



    //数组复制
    public static int[] copy(int[] array){
        int[] copyArray=new int[array.length];
        for(int i=0;i<array.length;i++){
            copyArray[i]=array[i];
        }
        return copyArray;
    }
    public static void main5(String[] args) {
        int[] array1={-1,2,39,118983,23,44444};
        int[] array2=copy(array1);
        System.out.println(Arrays.toString(array2));
    }



    //数组倒序
    public static void swap(int[] array,int i,int j){
        int temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
    public static void reserve(int[] array){
        int i=0;
        int j=array.length-1;
        while(i<j){
            swap(array,i,j);
            i++;
            j--;
        }
    }
    public static void main4(String[] args) {
        int[] array={-1,2,39,118983,23,44444};
        reserve(array);
        System.out.println(Arrays.toString(array));
    }



    //实现冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j <array.length-1-i ; j++) {
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
    }
    public static void main3(String[] args) {
        int[] array={-1,2,39,118983,23,44444};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }


    //给定一个整形数组求平均值
    public static double average(int[] array){
        double sum=0.0;//要是int的话，int/.length(这个也是int)，则最后也为int了，会舍弃小数部分，小数部分为0
        double ave=0.0;
        for(int x:array){
            sum+=x;
        }
        ave = sum / array.length;//如果sum为int，也可以在这里将sum强转成double
        return ave;
    }
    public static void main2(String[] args) {
        int[] array={1,2,4,51,3};
        double ave=average(array);
        System.out.println(ave);
    }



    //实现Arrays.toString
    public static String myToString(int[] array){
        String ret="[";
       /* for(int x:array){
            ret+=x+",";
        }
        ret=ret-",";这里会报错
        */
        for (int i = 0; i < array.length; i++) {
            if(i!=array.length-1) {
                ret += array[i] + ",";
            }else{
                ret+=array[i]+"]";
            }
        }
        return ret;
    }
    public static void main1(String[] args) {
        int[] array={1,3,2,4,98};
        String ret=myToString(array);
        System.out.println(ret);
    }
}
